<?php
namespace app\api\controller;
use think\Controller;
use think\Db;
class Auth extends Base{
	//身份认证
	public function auth_idcard(){
		if(!empty(input("param.token"))){
			$token=input("param.token");
		}else{
			return json(['code' => 501, 'msg' => '令牌呢？']);exit();
		}
		if(!empty(input("param.name"))){
			$data['name']=input("param.name");
		}else{
			return json(['code' => 502, 'msg' => '用户名不能为空']);exit();
		}
		if(!empty(input("param.idcard"))){
			$data['idcard']=input("param.idcard");
		}else{
			return json(['code' => 503, 'msg' => '身份证号不能为空']);exit();
		}
		$data['idcard_img']=input("param.idcard_img");
		$data['idcard_imgback']=input("param.idcard_imgback");
		$data['img']=input("param.img");
		$id=$this->do_mdecrypt($token);
        $res=Db::name('users')->where(array('id'=>$id))->update($data);
        if($res){
            return json(['code' => 200, 'msg' => '认证成功' ,'data' =>$res]);exit();
        }else{
            return json(['code' => 500, 'msg' => '认证失败']);exit();
        }
	}
	//身份认证结果
	public function get_idcard_auth(){
		if(!empty(input("param.token"))){
			$token=input("param.token");
		}else{
			return json(['code' => 501, 'msg' => '令牌呢？']);exit();
		}
		$id=$this->do_mdecrypt($token);
        $res=Db::name('users')->where(array('id'=>$id))->find();
        if($res){
        	if(!empty($res['name'])&&!empty($res['idcard'])&&!empty($res['idcard_img'])&&!empty($res['idcard_imgback'])&&!empty($res['img'])){
        		return json(['code' => 200, 'msg' => '认证成功' ,'name' =>$res['name'],'idcard' =>$res['idcard'],'idcard_img' =>$res['idcard_img'],'idcard_imgback' =>$res['idcard_imgback'],'img' =>$res['img']]);exit();
        	}else{
        		return json(['code' => 502, 'msg' => '认证结果' ,'name' =>$res['name'],'idcard' =>$res['idcard'],'idcard_img' =>$res['idcard_img'],'idcard_imgback' =>$res['idcard_imgback'],'img' =>$res['img']]);exit();
        	}
        }else{
            return json(['code' => 500, 'msg' => '查询失败']);exit();
        }
	}
	//联系人信息认证
	public function auth_contact(){
		if(!empty(input("param.token"))){
			$token=input("param.token");
		}else{
			return json(['code' => 501, 'msg' => '令牌呢？']);exit();
		}
		if(!empty(input("param.address")) && !empty(input("param.address_detail"))){
			$data['address']=input("param.address");
			$data['address_detail']=input("param.address_detail");
		}else{
			return json(['code' => 502, 'msg' => '地址不能为空']);exit();
		}
		if(!empty(input("param.email"))){
			$data['email']=input("param.email");
		}else{
			return json(['code' => 503, 'msg' => '邮箱不能为空']);exit();
		}
		if(!empty(input("param.wechat"))){
			$data['wechat']=input("param.wechat");
		}else{
			return json(['code' => 504, 'msg' => '微信号不能为空']);exit();
		}
		if(!empty(input("param.qq"))){
			$data['qq']=input("param.qq");
		}else{
			return json(['code' => 505, 'msg' => 'qq号不能为空']);exit();
		}
		if(!empty(input("param.relatives")) && !empty(input("param.relatives_phone"))){
			$data['relatives']=input("param.relatives");
			$data['relatives_phone']=input("param.relatives_phone");
		}else{
			return json(['code' => 506, 'msg' => '直系亲人电话或姓名不能为空']);exit();
		}
		if(!empty(input("param.partner")) && !empty(input("param.partner_phone"))){
			$data['partner']=input("param.partner");
			$data['partner_phone']=input("param.partner_phone");
		}else{
			return json(['code' => 507, 'msg' => '同事电话或姓名不能为空']);exit();
		}
		if(!empty(input("param.friend")) && !empty(input("param.friend_phone"))){
			$data['friend']=input("param.friend");
			$data['friend_phone']=input("param.friend_phone");
		}else{
			return json(['code' => 508, 'msg' => '朋友电话或姓名不能为空']);exit();
		}
		if(!empty(input("param.emer_con")) && !empty(input("param.emer_con_phone"))){
			$data['emer_con']=input("param.emer_con");
			$data['emer_con_phone']=input("param.emer_con_phone");
		}else{
			return json(['code' => 509, 'msg' => '紧急联系人电话或姓名不能为空']);exit();
		}
		$id=$this->do_mdecrypt($token);
            //return json(['code' => 201, 'msg' => '调试' ,'data' =>$id]);
        $res=Db::name('users')->where(array('id'=>$id))->update($data);
        if($res){
            return json(['code' => 200, 'msg' => '认证成功' ,'data' =>$res]);exit();
        }else{
            return json(['code' => 500, 'msg' => '认证失败']);exit();
        }
	}
	//联系人认证结果
	public function get_contact_auth(){
		if(!empty(input("param.token"))){
			$token=input("param.token");
		}else{
			return json(['code' => 501, 'msg' => '令牌呢？']);exit();
		}
		$id=$this->do_mdecrypt($token);
        $res=Db::name('users')->where(array('id'=>$id))->find();
        if($res){
        	if(!empty($res['address'])&&!empty($res['address_detail'])&&!empty($res['email'])&&!empty($res['wechat'])&&!empty($res['qq'])&&!empty($res['relatives'])&&!empty($res['relatives_phone'])&&!empty($res['partner'])&&!empty($res['partner_phone'])&&!empty($res['friend'])&&!empty($res['friend_phone'])&&!empty($res['emer_con'])&&!empty($res['emer_con_phone'])){
        		return json(['code' => 200, 'msg' => '认证成功' ,'address' =>$res['address'],'address_detail' =>$res['address_detail'],'email' =>$res['email'],'wechat' =>$res['wechat'],'qq' =>$res['qq'],'relatives' =>$res['relatives'],'relatives_phone' =>$res['relatives_phone'],'partner' =>$res['partner'],'partner_phone' =>$res['partner_phone'],'friend' =>$res['friend'],'friend_phone' =>$res['friend_phone'],'emer_con' =>$res['emer_con'],'emer_con_phone' =>$res['emer_con_phone']]);exit();
        	}else{
        		return json(['code' => 502, 'msg' => '认证结果' ,'address' =>$res['address'],'address_detail' =>$res['address_detail'],'email' =>$res['email'],'wechat' =>$res['wechat'],'qq' =>$res['qq'],'relatives' =>$res['relatives'],'relatives_phone' =>$res['relatives_phone'],'partner' =>$res['partner'],'partner_phone' =>$res['partner_phone'],'friend' =>$res['friend'],'friend_phone' =>$res['friend_phone'],'emer_con' =>$res['emer_con'],'emer_con_phone' =>$res['emer_con_phone']]);exit();
        	}
        }else{
            return json(['code' => 500, 'msg' => '查询失败']);exit();
        }
	}
	//公司信息认证
	public function auth_company(){
		if(!empty(input("param.token"))){
			$token=input("param.token");
		}else{
			return json(['code' => 501, 'msg' => '令牌呢？']);exit();
		}
		if(!empty(input("param.company_name"))){
			$data['company_name']=input("param.company_name");
		}else{
			return json(['code' => 502, 'msg' => '公司名称不能为空']);exit();
		}
		if(!empty(input("param.company_address"))){
			$data['company_address']=input("param.company_address");
		}else{
			return json(['code' => 503, 'msg' => '公司地址不能为空']);exit();
		}
		if(!empty(input("param.industry"))){
			$data['industry']=input("param.industry");
		}else{
			return json(['code' => 504, 'msg' => '所属行业不能为空']);exit();
		}
		if(!empty(input("param.work_age"))){
			$data['work_age']=input("param.work_age");
		}else{
			return json(['code' => 505, 'msg' => '工作年限不能为空']);exit();
		}
		$id=$this->do_mdecrypt($token);
            //return json(['code' => 201, 'msg' => '调试' ,'data' =>$id]);
        $res=Db::name('users')->where(array('id'=>$id))->update($data);
        if($res){
            return json(['code' => 200, 'msg' => '认证成功' ,'data' =>$res]);exit();
        }else{
            return json(['code' => 500, 'msg' => '认证失败']);exit();
        }
	}
	//公司信息认证结果
	public function get_company_auth(){
		if(!empty(input("param.token"))){
			$token=input("param.token");
		}else{
			return json(['code' => 501, 'msg' => '令牌呢？']);exit();
		}
		$id=$this->do_mdecrypt($token);
        $res=Db::name('users')->where(array('id'=>$id))->find();
        if($res){
        	if(!empty($res['company_name'])&&!empty($res['company_address'])&&!empty($res['industry'])&&!empty($res['work_age'])){
        		return json(['code' => 200, 'msg' => '认证成功' ,'company_name' =>$res['company_name'],'company_address' =>$res['company_address'],'industry' =>$res['industry'],'work_age' =>$res['work_age']]);exit();
        	}else{
        		return json(['code' => 502, 'msg' => '认证结果' ,'company_name' =>$res['company_name'],'company_address' =>$res['company_address'],'industry' =>$res['industry'],'work_age' =>$res['work_age']]);exit();
        	}
        }else{
            return json(['code' => 500, 'msg' => '查询失败']);exit();
        }
	}
	//其他认证
	public function auth_other(){
		if(!empty(input("param.token"))){
			$token=input("param.token");
		}else{
			return json(['code' => 501, 'msg' => '令牌呢？']);exit();
		}
		if(!empty(input("param.other"))){
			$data['other']=input("param.other");
		}else{
			return json(['code' => 502, 'msg' => '照片不能为空']);exit();
		}
		$id=$this->do_mdecrypt($token);
        $res=Db::name('users')->where(array('id'=>$id))->update($data);
        if($res){
            return json(['code' => 200, 'msg' => '认证成功' ,'data' =>$res]);exit();
        }else{
            return json(['code' => 500, 'msg' => '认证失败']);exit();
        }
	}
	//认证总结果
	public function get_auths(){
		if(!empty(input("param.token"))){
			$token=input("param.token");
		}else{
			return json(['code' => 501, 'msg' => '令牌呢？']);exit();
		}
		$result['contact']=0;
        $result['idcard']=0;
        $result['work']=0;
        $result['bankcard']=0;
        $result['phoneinfo']=0;
        $result['other']=0;
        $result['sesame_state']=0;
		$id=$this->do_mdecrypt($token);
        $res=Db::name('users')->where(array('id'=>$id))->find();
        if($res){
        	if(!empty($res['address'])&&!empty($res['address_detail'])&&!empty($res['email'])&&!empty($res['wechat'])&&!empty($res['qq'])&&!empty($res['relatives'])&&!empty($res['relatives_phone'])&&!empty($res['partner'])&&!empty($res['partner_phone'])&&!empty($res['friend'])&&!empty($res['friend_phone'])&&!empty($res['emer_con'])&&!empty($res['emer_con_phone'])){
        		$result['contact']=1;
        	}
        	if(!empty($res['name'])&&!empty($res['idcard'])&&!empty($res['idcard_img'])&&!empty($res['idcard_imgback'])&&!empty($res['img'])){
        		$result['idcard']=1;
        	}
        	if(!empty($res['company_name'])&&!empty($res['company_address'])&&!empty($res['industry'])&&!empty($res['work_age'])){
        		$result['work']=1;
        	}
        	if(!empty($res['bank_name'])&&!empty($res['bank_idcard'])&&!empty($res['bank_number'])&&!empty($res['bank_phone'])&&!empty($res['bank_cdid'])){
        		$result['bankcard']=1;
        	}
        	if($res['yys_state']==1||$res['yys_state']==2){
        		$result['phoneinfo']=1;
        	}
        	if($res['sesame_state']==1){
        		$result['sesame_state']=1;
        	}
        	if(!empty($res['other'])){
        		$result['other']=1;
        	}
            return json(['code' => 200, 'msg' => '查询成功','result'=>$result]);exit();
        }else{
            return json(['code' => 500, 'msg' => '查询失败']);exit();
        }
	}
	//获取通讯录
	public function getBook(){
		if(!empty(input("param.token"))){
			$token=input("param.token");
		}else{
			return json(['code' => 501, 'msg' => '令牌呢？']);exit();
		}
		if(!empty(input("param.list"))){
			$list=input("param.list");
		}else{
			return json(['code' => 502, 'msg' => '联系人不能为空']);exit();
		}
		$id=$this->do_mdecrypt($token);
	    $insert=[
		    'user_id'=>$id,
			'list'=>json_encode($list['booklist']),
			'time'=>time()
		];
		$res=Db::name('contacts')->insert($insert);
		if($res){
			return json(['code' => 200, 'msg' => '保存成功']);exit();
		}else{
			return json(['code' => 500, 'msg' => '保存失败']);exit();
		}
	}
	//解密
	private function do_mdecrypt($string, $key=''){
		$key= !empty($key) ? $key : config('byzk.KEY');
		$operation = 'D';
	   	$key=md5($key); 
	  	$key_length=strlen($key); 
	   	$string=$operation=='D'?base64_decode($string):substr(md5($string.$key),0,8).$string; 
		$string_length=strlen($string); 
		$rndkey=$box=array(); 
		$result=''; 
		for($i=0;$i<=255;$i++){ 
		    $rndkey[$i]=ord($key[$i%$key_length]); 
		    $box[$i]=$i; 
		} 
		for($j=$i=0;$i<256;$i++){ 
		    $j=($j+$box[$i]+$rndkey[$i])%256; 
		    $tmp=$box[$i]; 
		    $box[$i]=$box[$j]; 
		    $box[$j]=$tmp; 
		} 
		for($a=$j=$i=0;$i<$string_length;$i++){ 
		    $a=($a+1)%256; 
		    $j=($j+$box[$a])%256; 
		    $tmp=$box[$a]; 
		    $box[$a]=$box[$j]; 
		    $box[$j]=$tmp; 
		    $result.=chr(ord($string[$i])^($box[($box[$a]+$box[$j])%256])); 
		} 
		if($operation=='D'){ 
		    if(substr($result,0,8)==substr(md5(substr($result,8).$key),0,8)){ 
		    	return substr($result,8); 
		    }else{ 
		    	return''; 
		    } 
		}else{ 
		    return str_replace('=','',base64_encode($result)); 
		} 
	}
}